﻿// JavaScript Document 30/06
/* #################################### xu ly email #############################*/
var chkEmailErr = 0;
var chkDomain = 0;
var curEmailname = '';
var curDomain = '';
var chkDomainSel = 0;
/* #################################### xu ly username #############################*/
var chkAccErr = 0;
var curAcc = '';
function show_mess_game_v2(){
	//var is_game = $('#is_game').val();
	var is_game = 1;
	var sUserName = $('#sUserName').val();
	if(is_game == 1){
		if(sUserName.indexOf('.') != -1 || sUserName.indexOf('_') != -1){
			$('#mess_game_v2').show();
		}else{
			$('#mess_game_v2').hide();
		}		
	}
	return;
}
function checkErrorKeyUp(){	
	var username = $('#sUserName').val();
	if(!acc_checkCharBegin(username)){
		chkAccErr = 1;
		showError('#sUserName',UserName_Error[0]);
		return 1;
	}	
	if(!acc_checkCharContinuous(username)){
		chkAccErr = 1;
		showError('#sUserName',UserName_Error[2]);
		return 1;
	}
	if(!acc_checkSpecialChars(username)){
		chkAccErr = 1;
		showError('#sUserName',UserName_Error[3]);
		return 1;
	}	
	return 0;
}
function checkErrorBlur(){
	var username = $('#sUserName').val();
	if(!acc_checkCharEnd(username)){
		chkAccErr = 1;
		showError('#sUserName',UserName_Error[1]);
		return 1;
	}
	if(!acc_checkLen(username)){
		chkAccErr = 1;
		showError('#sUserName',UserName_Error[4]);	
		return 1;		
	}	
	if(checkStringIsNum(username)){
		chkAccErr = 1;
		showError(this,UserName_Error[5]);
		return 1;
	}		
	return 0;
}
$("#sUserName").keyup( function() {
	show_mess_game_v2();
	if(checkErrorKeyUp()) return;
	if($("#sUserName_note").css('display') == 'none'){
		chkAccErr = 0;
		hideError('#sUserName');
		return 0;
	}
} );
$("#sUserName").blur( function(){
	//return;						   	
	if(!acc_checkCharEnd(this.value)){
		chkAccErr = 1;
		return showError(this,UserName_Error[1]);
	}
	if(chkAccErr==1){
		return false;
	}	
	if(!acc_checkLen(this.value)){
		return showError(this,UserName_Error[4]);	
	}	
	if(this.value!=''){
		if(checkStringIsNum(this.value)){
			chkAccErr = 1;
			return showError(this,UserName_Error[5]);
		}	
		getAccountName(1);
	}	
	else{
		//closeSuggest();	
	}	
	//return checkValid("#sUserName_icon");
	
} );
function callGetsuggestion(){
	getAccountName(1,1);
}
/////////////
var chkPassErr = 0;
var chkConfirmPassErr = 0;
function resetMeta(){
	$('#meta1').removeClass('meta');	
	$('#meta2').removeClass('meta');		
	$('#meta3').removeClass('meta');		
	$('#meta4').removeClass('meta');	
}
$("#sPassWord1").keyup( function() {
	//Update check							 
	if(check_show_keyboard()) return true;
	//End
	$("#sConfirm_PassWord1").val('');
	if(!checkVietNam(this.value)){
		chkPassErr = 1;
		resetMeta();
		return showError(this,Password_Error[0]);
	}else{
		if($("#sPassWord1_note").css('display') == 'none'){
			chkPassErr = 0;
			hideError(this);
		}
	}
	if(this.value.length < 6){
		resetMeta();
		return false;
	}	
	show_Meta(this.value);	
} );
$("#sPassWord1").blur( function(){
	//Update check							 
	if(check_show_keyboard()) return true;
	//End			
	if(this.value == ''){
		return false;
	}	
	if(chkPassErr==1){
		$("#sConfirm_PassWord1").focus();
		return false;
	}	
	if(!pass_checkLen(this.value)){
		chkPassErr=1;
		return showError(this,'Phải có ít nhất 6 ký tự.');	
		return false;
	}	
	chkPassErr=0;
	$("#sPassWord1_tr_note").removeClass();
	$("#sPassWord1_note").hide();
	show_Meta(this.value);
	if(!STRONG_PASSWORD){
		chkPassErr=1;
		return showError(this,PASSWORD_ERR_NEW);
	}
	return checkValid("#sPassWord1_icon");	
} );
$("#sConfirm_PassWord1").keyup( function() {	
	//Update check							 
	if(check_show_keyboard()) return true;
	//End					 
	if( this.value.length > 5 & this.value != "" &&  this.value.length == $("#sPassWord1").val().length){
		if($("#sPassWord1").val() == $("#sConfirm_PassWord1").val()){
			chkConfirmPassErr = 0;
			hideError(this);
			return checkValid("#sConfirm_PassWord1_icon");				
		}
	}
	if(this.value.length >= $("#sPassWord1").val().length){
		if($("#sPassWord1").val() != $("#sConfirm_PassWord1").val()){
			chkConfirmPassErr = 1;
			return showError(this,Password_Error[2]);
			return false;
		}
	}
} );

$("#sConfirm_PassWord1").blur( function(){
	//Update check							 
	if(check_show_keyboard()) return true;
	//End									
	if($("#sConfirm_PassWord1").val()=='' || $("#sPassWord1").val()=='' || chkPassErr==1)
		return false;
	if($("#sPassWord1").val() != $("#sConfirm_PassWord1").val()){
		chkConfirmPassErr = 1;
		return showError(this,Password_Error[2]);
		return false;
	}
	chkConfirmPassErr = 0;
	hideError(this);
	return checkValid("#sConfirm_PassWord1_icon");
} );
var chkDateErr = 0;
var dd = '';
var mm = '';
var yyyy = '';
var chkSelDate = 0;
function getDate_(){
	dd = $("#dName").val();
	mm = $("#mName").val();
	yyyy = $("#yName").val();
}
var Date_invalid = 'Ngày sinh không hợp lệ.';
$("#yName").blur( function() {
	chkSelDate = 1;	
	getDate_();
	//////////
	if(this.value == '-1' && dd=='-1' && mm == -1){
		return false;
	}
	if(!checkDate(dd,mm,yyyy) ){
		chkDateErr = 1;
		return showError('#dName',Persional_Error[1]);
	}
	chkDateErr = 0;
	hideError('#dName');
	return checkValid("#dName_icon");	
} );
var keycode = '';
$("#sVerifyCode").keyup( function() {
	document.onkeydown = checkKeycode
	if(keycode == 13) return;
	if( trimString(this.value).length == 6){
		checkImage();
	}
} );
function checkKeycode(e) {
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
}
function onClickLogin(){
	document.location = "http://mail.zing.vn";	
}
$("#fullname").blur( function() {	
	if( trimString($("#fullname").val()) != ''){
		return checkValid("#fullname_icon");
	}						   	
} );
//check Email
/*Check password*/
$("#email_name").focus( function() {
	//hide_description('email_name');							 
	if(this.value == ''){
		hideError("#email_name");
		$("#email_name_tr_note").addClass("td_note");
		$("#email_name_note").show();
	}else{
		//$(des).hide();
	}	
} );
$("#email_name").blur( function() {
	$('#email_name_note').hide();
	$("#email_name_tr_note").removeClass("td_note");
	if(!check_email_name(this))
		return false;
	hideError(this);
} );
//update 04/04/2011
var email_err = 'Địa chỉ email không hợp lệ.';
function check_email_name(obj){	
	var fullName = $("#email_name").val();
	if(!fullName) return false;
	var tmp = explode ("@",fullName);
	var mailName = tmp[0];
	var mailDomain = tmp[1];
	if(!mailDomain){
		showError(obj,email_err);
		return false;	
	}	
	if(mailDomain=='zing.vn' && mailName == $('#sUserName').val()) {	
		showError(obj,'Không được trùng tên đăng nhập.');
		return false;	
	}	
	if(!CheckEmail(mailName,mailDomain,2,4,7,100)){
		showError(obj,email_err);
		return false;
	}
	hideError(obj);
	return true;
}
function CheckEmail(mailName,mailDomain,minlenMailName,minlenMailDomain,minlen,maxlen)
{
	var sMailName = mailName.toLowerCase();
	var sMailDomain = mailDomain.toLowerCase();
	if(!sMailName || !sMailDomain)
		return false;
	if(sMailName.length < minlenMailName || sMailName.charAt(0) == '_' || sMailName.charAt(sMailName.length-1) == '_' || sMailName.indexOf('._') != -1 || sMailName.indexOf('_.') != -1 || sMailName.indexOf('__') != -1 || sMailName.indexOf('-') != -1)
	{ 
		
		return false;
	}
	if(sMailDomain.length < minlenMailDomain || sMailDomain.charAt(0) == '-' || sMailDomain.charAt(sMailDomain.length-1) == '-' || sMailDomain.indexOf('.-') != -1 || sMailDomain.indexOf('-.') != -1 || sMailDomain.indexOf('--') != -1 || sMailDomain.indexOf('_') != -1)
	{ 
		
		return false;
	}
	//		
	var stringIn = sMailName+'@'+sMailDomain ;
	//
	if(stringIn.length < minlen || stringIn.length > maxlen)
	{
		
		return false;
	}
	//
	if (stringIn.indexOf('..') != -1 || stringIn.indexOf('.@') != -1 || stringIn.indexOf('@.') != -1 || stringIn.indexOf(':') != -1  )
	{
		
		return false;
	}
	var re = /^([A-Za-z0-9\_\-]+\.)*[A-Za-z0-9\_\-]+@[A-Za-z0-9\_\-]+(\.[A-Za-z0-9\_\-]+)+$/;
	if (stringIn.search(re) == -1)
	{
		
		return false;
	}		
	return true;
}

